package statistics.poznavaniTextu;

import Jama.EigenvalueDecomposition;
import Jama.Matrix;
import com.sun.corba.se.spi.orbutil.fsm.Input;
import sun.awt.geom.Curve;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Arrays;

/*
*   File Elipsa 
*   in package statistics.poznavaniTextu
*   was created by slaha
*   on 28.4.12 at 23:45.
*
*/
public class Elipsa {
  static boolean pis = true;
  final static double KRITICKA_HODNOTA = 5.99;
  public Point teziste;
  double[] osy = new double[2];
  //double[] vlV1, vlV2;
  double[][] vlV;
  Graphics H;
  double pxX, pxY;
  ArrayList<PointBasen> body = new ArrayList<>();

 public Elipsa(double x, double y, double osaA, double osaB, double[][] vlV, double pxX, double pxY) {
    teziste = new Point((int) Math.round(x), (int) Math.round(y));
    osy[0] = osaA;
    osy[1] = osaB;

    //this.vlV1 = vlV2;
    //this.vlV2 = vlV1;

   this.vlV = vlV;
    this.pxX = pxX;
    this.pxY = pxY;
  }


  public ArrayList<PointBasen> getBody() {
    return body;
  }

  public void paint2(Graphics2D g) {
    Point t = new Point((int) g.getTransform().getTranslateX() + teziste.x, (int) g.getTransform().getTranslateY() + teziste.y);
    g.fillOval((int) teziste.x - 3, (int) (teziste.y - 3), 6, 6);
    int puvX = (int) Math.round(g.getTransform().getTranslateX());
    int puvY = (int) Math.round(g.getTransform().getTranslateY());
    g.translate(-g.getTransform().getTranslateX(), -g.getTransform().getTranslateY());

    g.translate(t.x, t.y);

    final int rozmer = (int) (1000 * Math.PI * 2);
    double[] tt = new double[rozmer];

    for (int i = 0; i < tt.length; i++) {
      tt[i] = i * 0.001;
    }

    double[] xt = new double[rozmer];
    double[] yt = new double[rozmer];

    for (int i = 0; i < tt.length; i++) {
      xt[i] = osy[0] * Math.cos(tt[i]);
      yt[i] = osy[1] * Math.sin(tt[i]);
    }

    double[] yy1 = new double[rozmer];
    double[] xx1 = new double[rozmer];

  /*  for (int i = 0; i < yy1.length; i++) {
      yy1[i] = (vlV1[1] * xt[i] - vlV1[0] * yt[i]) /
          (vlV1[1] * vlV2[0] - vlV1[0] * vlV2[1]);

      xx1[i] = (xt[i] - vlV2[0] * yy1[i]) / vlV1[0];
    }
*/

    int kx, ky;
    int nakresleneZX = (int) Math.round(xx1[0]);
    int nakresleneZY = (int) Math.round(yy1[0]);

    
    for (int i = 1; i < yy1.length; i++) {

      kx = (int) Math.round(xx1[i]);
      ky = (int) Math.round(yy1[i]);

     
        g.drawLine(nakresleneZX, nakresleneZY, kx, ky);
        System.out.println(nakresleneZX + ";" + nakresleneZY + " -> " + kx + ";" + ky);
        nakresleneZX = kx;
        nakresleneZY = kx;


    

     // g.drawLine(kx, ky, kx, ky);
      //g.draw(q);
    }

  /*  zx = (int) Math.round(xx1[xx1.length-1] * pxX);
    zy = (int) Math.round(yy1[yy1.length-1] * pxY);
    kx = (int) Math.round(xx1[0] * pxX);
    ky = (int) Math.round(yy1[0] * pxY);

    g.drawLine(zx, zy, kx, ky);
    */
    g.translate(-t.x, -t.y);
    g.translate(puvX, puvY);
  }

  public void paint(Graphics2D g) {
    Point t = new Point((int) g.getTransform().getTranslateX() + teziste.x, (int) g.getTransform().getTranslateY() + teziste.y);
    g.fillOval((int) teziste.x - 2, (int) (teziste.y - 2), 4, 4);
    int puvX = (int) Math.round(g.getTransform().getTranslateX());
    int puvY = (int) Math.round(g.getTransform().getTranslateY());
    g.translate(-g.getTransform().getTranslateX(), -g.getTransform().getTranslateY());
    g.translate(t.x, t.y);
    try {
      FileWriter fstream = new FileWriter("out.txt");
      BufferedWriter out = new BufferedWriter(fstream);
      out.write("\n\nZACATEK\n");


      //.. ELIPSA2.m
      double xt, yt;
      double yy1, xx1;
      double xx, yy;
      PointBasen predchozi = null;
      //výpočet transformované elipsy xt^2 / a + yt ^ 2 / b = 1,
      // kde xt = v1 '*[x,y]' a yt = v2'*[x,y]'
      for (double i = 0d; i <= 2*Math.PI; i+=0.001) {
        xt = osy[0] *  Math.cos(i);
        yt = osy[1] *  Math.sin(i);

        /*yy1 = (vlV1[1] * xt - vlV1[0] * yt) /
            ((vlV1[1] * vlV2[0] - vlV1[0] * vlV2[1]));

        xx1 = (xt - vlV2[0] * yy1) / vlV1[0];
*/

      yy1 = (vlV[0][1] * xt - vlV[0][0] * yt) /
            ((vlV[0][1] * vlV[1][0] - vlV[0][0] * vlV[1][1]));

        xx1 = (xt - vlV[1][0] * yy1) / vlV[0][0];

        
        xx = /*teziste.x */ (xx1 != Double.NaN) ? xx1 : 0;
        yy = /*teziste.y - */yy1;

        PointBasen bod = new PointBasen(xx, yy);
        if (predchozi != null) {
          int zx, zy, kx, ky;
          zx = (int) Math.round(predchozi.x);
          zy = (int) Math.round(predchozi.y);
          kx = (int) Math.round(bod.x);
          ky = (int) Math.round(bod.y);
          if (pis) {

            out.write("[" + predchozi.x + ";" + predchozi.y + "] -> [" + bod.x + ";" + bod.y + "]\n");
            

          }
          g.drawLine(zx, zy, kx, ky);


          predchozi = bod;
        } else {
          predchozi = bod;
        }



  //      g.drawOval((int) (xx*pxX), (int) (yy*pxY), 3, 3);
        //PointBasen bod = new PointBasen(xx, yy);
        //body.add(bod);
      }

      out.write("\n\nKONEC\n");
      out.flush();
      out.close();
    } catch (Exception  e) {//Catch exception if any
      System.err.println("Error: " + e.getMessage());
    }

    
    g.translate(-t.x, -t.y);
    g.translate(puvX, puvY);
  }
}